package com.sleep.psy.external_server.business.sys.service.impl;

import com.sleep.psy.external_server.business.main.entity.MainDistributer;
import com.sleep.psy.external_server.business.main.service.MainDistributerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sleep.psy.external_server.business.sys.entity.SysDept;
import com.sleep.psy.external_server.business.sys.entity.SysUser;
import com.sleep.psy.external_server.business.sys.mapper.SysDeptMapper;
import com.sleep.psy.external_server.business.sys.mapper.SysUserMapper;
import com.sleep.psy.external_server.business.sys.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author wenliguo
 * @since 2025-08-12
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    @Autowired
    private MainDistributerService mainDistributerService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 通过用户id获取部门信息
     * @author wenliguo
     * @date 2025/8/12 13:50
     * @param userId 用户id(患者)
     * @return com.sleep.psy.external_server.sys.entity.SysDept
     */
    @Override
    public SysDept selectDept(Long userId) {
        MainDistributer distributer = mainDistributerService.selectByUserId(userId);
        if (null == distributer) {
            return null;
        }

        Long realAccountId = distributer.getRealAccountId();
        if (null == realAccountId) {
            return null;
        }

        SysUser sysUser = sysUserMapper.selectById(realAccountId);
        if (null == sysUser) {
            return null;
        }

        Long deptId = sysUser.getDeptId();
        if (null == deptId) {
            return null;
        }

        return sysDeptMapper.selectById(deptId);

    }
}
